package com.dhcc.DRGsHosp.facade.impl;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Named;

import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.QueryChannelService;
import org.dayatang.utils.Page;
import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.DRGsApplication;
import com.dhcc.DRGsHosp.core.domain.DRGs;
import com.dhcc.DRGsHosp.facade.DRGsFacade;
import com.dhcc.DRGsHosp.facade.DrgsIndexCalFacade;
import com.dhcc.DRGsHosp.facade.dto.DRGsDTO;
import com.dhcc.DRGsHosp.facade.dto.IndexCalDTO;
import com.dhcc.DRGsHosp.facade.impl.assembler.DRGsAssembler;
import com.dhcc.DRGsHosp.facade.impl.assembler.IndexCalAssembler;

@Named("drgsHospFacadeImpl15")
public class DRGsFacadeImpl implements DRGsFacade {

	@Autowired
	private DRGsApplication  application;
	
	@Autowired
	private DrgsIndexCalFacade  drgsIndexCalFacade;
	
	private QueryChannelService queryChannel;

    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
	
	public InvokeResult getDRGs(String drgCode) {
		return InvokeResult.success(DRGsAssembler.toDTO(application.getDRGs(drgCode)));
	}
	
	public InvokeResult creatDRGs(DRGsDTO dRGsDTO) {
		application.creatDRGs(DRGsAssembler.toEntity(dRGsDTO));
		return InvokeResult.success();
	}
	
	public InvokeResult updateDRGs(DRGsDTO dRGsDTO) {
		application.updateDRGs(DRGsAssembler.toEntity(dRGsDTO));
		return InvokeResult.success();
	}
	
	public InvokeResult removeDRGs(String drgCode) {
		application.removeDRGs(application.getDRGs(drgCode));
		return InvokeResult.success();
	}
	
	public InvokeResult removeDRGss(String[] drgCodes) {
		Set<DRGs> dRGss= new HashSet<DRGs>();
		for (String drgCode : drgCodes) {
			dRGss.add(application.getDRGs(drgCode));
		}
		application.removeDRGss(dRGss);
		return InvokeResult.success();
	}
	
	public List<DRGsDTO> findAllDRGs(DRGsDTO dto) {
		return DRGsAssembler.toDTOs(application.findAllDRGs(DRGsAssembler.toEntity(dto)));
	}
	
	public Page<DRGsDTO> pageQueryDRGs(DRGsDTO queryVo, int currentPage, int pageSize) {
		List<Object> conditionVals = new ArrayList<Object>();
	   	StringBuilder jpql = new StringBuilder("select _dRGs from DRGs _dRGs   where 1=1 ");
	   	if (queryVo.getDrgCode() != null && !"".equals(queryVo.getDrgCode())) {
	   		jpql.append(" and _dRGs.drgCode like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDrgCode()));
	   	}		
	   	if (queryVo.getDrgDesc() != null && !"".equals(queryVo.getDrgDesc())) {
	   		jpql.append(" and _dRGs.drgDesc like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDrgDesc()));
	   	}		
	   	if (queryVo.getDrgWeight() != null && !"".equals(queryVo.getDrgWeight())) {
	   		jpql.append(" and _dRGs.drgWeight like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDrgWeight()));
	   	}		
	   	if (queryVo.getDrgLrm() != null && !"".equals(queryVo.getDrgLrm())) {
	   		jpql.append(" and _dRGs.drgLrm like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDrgLrm()));
	   	}		
	   	if (queryVo.getDrg() != null && !"".equals(queryVo.getDrg())) {
	   		jpql.append(" and _dRGs.drg like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDrg()));
	   	}		
	   	if (queryVo.getAdrgCode() != null && !"".equals(queryVo.getAdrgCode())) {
	   		jpql.append(" and _dRGs.adrgCode like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getAdrgCode()));
	   	}		
	   	if (queryVo.getVerCode() != null && !"".equals(queryVo.getVerCode())) {
	   		jpql.append(" and _dRGs.verCode like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getVerCode()));
	   	}		
        Page<DRGs> pages = getQueryChannelService()
		   .createJpqlQuery(jpql.toString())
		   .setParameters(conditionVals)
		   .setPage(currentPage, pageSize)
		   .pagedList();
		   
        return new Page<DRGsDTO>(pages.getStart(), pages.getResultCount(),pageSize, DRGsAssembler.toDTOs(pages.getData()));
	}

	@Override
	public InvokeResult getDrgsByParam(IndexCalDTO dto) {
	    //获取分组方案
	    String verRegion = drgsIndexCalFacade.getGroupScheme();
	    dto.setVerRegion(verRegion);
	    
		List<Map<String,Object>> drglist  = application.findDrgsByPar(IndexCalAssembler.toEntity(dto));
		return InvokeResult.success(drglist);
	}
	
	
}
